package reflection;

import java.lang.reflect.AnnotatedType;

/**
 * @author 小小唐
 * @Date 2022/8/25-星期四-8:42
 *
 * 反射获得对象的几种方式
 */
public class Demo2 {

    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是"+person.name);

        //方式一：通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        //方式二：通过forName获得
        Class c2 = Class.forName("reflection.Student");
        System.out.println(c2.hashCode()+"\t"+c2.getName());

        //方式三：通过类名.class获得
        Class c3 = Student.class;
        System.out.println(c3.hashCode()+"\t"+c3.equals(person.name));

        //方式四：基本内置类型的包装类都有一个Type属性
        Class c4 = Integer.TYPE;
        System.out.println(c4+"\t"+c4.hashCode());

        //方式五：通过获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5+"\t"+c5.equals(c3)+c5.hashCode());

    }

}


class Person {

    public String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person{
    public Student(){
        this.name = "学生";
    }
}

class Teacher extends Person{
    public Teacher(){
        this.name = "教师";
    }
}



